package scm.inventoryControl.managedbean;

import crm.salesOrderProcessing.entity.DeliveryOrderEntity;
import java.io.IOException;
import java.io.Serializable;
import java.util.logging.Logger;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.faces.event.PhaseEvent;
import mrp.productionPlanning.managedbean.EditForecastTableManagedBean;
import org.primefaces.event.FlowEvent;

@ManagedBean
@ViewScoped
public class GenerateOrdersResultManagedBean implements Serializable {

    private DeliveryOrderEntity selectedDeliveryOrder;
    private boolean skip;
    private static Logger logger = Logger.getLogger(EditForecastTableManagedBean.class.getName());

    public GenerateOrdersResultManagedBean() {
    }

    public void initView(PhaseEvent event) {
        if (getSelectedDeliveryOrder() == null) {
            setSelectedDeliveryOrder((DeliveryOrderEntity) FacesContext.getCurrentInstance().getExternalContext().getFlash().get("selectedDeliveryOrder"));
            System.out.println("*****************************" + selectedDeliveryOrder.getPickingOrder().getId());
        }
    }

    public String onFlowProcess(FlowEvent event) {
        logger.info("Current wizard step:" + event.getOldStep());
        logger.info("Next step:" + event.getNewStep());

        if (skip) {
            skip = false;   //reset in case user goes back  
            return "confirm";
        } else {
            return event.getNewStep();
        }
    }
    
    public void goBack(ActionEvent event) throws IOException{
        FacesContext.getCurrentInstance().getExternalContext().redirect("generateOrders.xhtml");
    }

    public DeliveryOrderEntity getSelectedDeliveryOrder() {
        return selectedDeliveryOrder;
    }

    public void setSelectedDeliveryOrder(DeliveryOrderEntity selectedDeliveryOrder) {
        this.selectedDeliveryOrder = selectedDeliveryOrder;
    }
}
